/*
 * @Description: 绘图模式下功能模块-圆
 * @Date: 2022-02-10 16:12:15
 * @Author: yeming
 */

import { fromLonLat, toLonLat, transform } from 'ol/proj.js'
// 一个source下含多个Feature。Feature是所有图形的一个封装，具体代表什么图形由内部的geometry参数决定。
import OlFeature from 'ol/Feature'
import OlGeomCircle from 'ol/geom/Circle';

import { setOlStyleCircle, setOlStyleCircleNoFill } from '../../common/olStyle.js'

// 默认蓝色区域
const DEFAULT_CIRCLE_STYLE = { 'fillColor': 'rgba(0, 153, 255, 0.1)', 'strokeColor': 'rgba(0, 153, 255, 1)', 'strokeWidth': 2 }
// 绿色区域
const GREEN_CIRCLE_STYLE = { 'fillColor': 'rgba(0, 102, 102, 0.2)', 'strokeColor': 'rgba(0, 102, 102, 1)', 'strokeWidth': 2 }
// 禁飞区红色
const FORBID_CIRCLE_STYLE = { 'fillColor': 'rgba(255, 102, 102, 0.2)', 'strokeColor': 'rgba(255, 102, 102, 1)', 'strokeWidth': 1 }
// 电子围栏橙色区域
const FENCE_CIRCLE_STYLE = { 'fillColor': 'rgba(251, 172, 29,0.2)', 'strokeColor': 'rgba(251, 172, 29,1)', 'strokeWidth': 1 }
// 默认半径
const DEFAULT_CIRCLE_RADIUS = 160

class Circle3d {
  // type 1-单线飞行和机载航线中的航点 2-指点飞行的点 3-自定义点 4-吊舱视野位置区域 5-禁飞区 6-电子围栏
  constructor(entities, id, type, latlng, radius, style, clampToGround = false) {
    let that = this
    let customStyle = {}
    if (style === null || JSON.stringify(style) == "{}") {
      if (type <= 3) customStyle = DEFAULT_CIRCLE_STYLE
      if (type == 4) customStyle = GREEN_CIRCLE_STYLE
      if (type == 5 || type == 6) customStyle = FORBID_CIRCLE_STYLE
    } else customStyle = style

    this.entities = entities
    this.position = new Cesium.Cartesian3.fromDegrees(latlng[0], latlng[1])
    this.options = {
      id: id, // 创建和2d飞机相同sn的entity,
      name: 'Circle3d_' + id,
      type: type,
      show: true,
      position: new Cesium.CallbackProperty(function () { // 实时更新 CallbackProperty 第一个参数为回调函数
        // console.log(that.positions)
        return that.position
      }, false),
      ellipse: {
        material: Cesium.Color.fromCssColorString(customStyle.fillColor).withAlpha(0.2),
        semiMinorAxis: radius,
        semiMajorAxis: radius,
        clampToGround: clampToGround,
        height: latlng[2],
        // extrudedHeight: extrudedHeight,
        outline: true,
        outlineColor: Cesium.Color.fromCssColorString(customStyle.strokeColor),
        outlineWidth: customStyle.strokeWidth
      }
    }
    this.entity = this.entities.add(this.options)
  }
}

class Circle {
  // type 1-单线飞行和机载航线中的航点 2-指点飞行的点 3-自定义点 4-吊舱视野位置区域 5-禁飞区 6-电子围栏
  constructor(ol2d, vectorSource) {
    this.ol2d = ol2d
    this.vectorSource = vectorSource // 矢量标注的数据源 addFeature(), getFeatures(), removeFeature()
    this.feature = null
  }

  /**
   * @desc: 添加circle
   * @param {Number} id
   * @param {Number} type 1-单线飞行和机载航线中的航点 2-指点飞行的点 3-自定义点 4-吊舱视野位置区域 5-禁飞区 6-电子围栏
   * @param {Array} latlng 圆心经纬度 [lng, lat, h]
   * @param {Number} isHover 是否盘旋 0 1
   * @param {Number} radius 圆心半径
   * @param {Object} style
   * @param {Boolean} rend3d 是否同步渲染3d
   */
  addCircle (id, type = 1, latlng, isHover = 1, radius = DEFAULT_CIRCLE_RADIUS, style = DEFAULT_CIRCLE_STYLE, rend3d = false) {
    let customStyle = {}
    if (style === null || JSON.stringify(style) == "{}") {
      if (type <= 3) customStyle = DEFAULT_CIRCLE_STYLE
      if (type == 4) customStyle = GREEN_CIRCLE_STYLE
      if (type == 5) customStyle = FORBID_CIRCLE_STYLE
      if (type == 6) customStyle = FENCE_CIRCLE_STYLE
    } else customStyle = style

    // 传过来的半径是几何半径（直线），需要转化为弧度半径（曲面），余弦公式推导
    let arcRadius = radius / Math.cos(latlng[1] / 180 * Math.PI)
    let feature = new OlFeature({
      id: id,
      sessionId: id,
      type: type,
      latlng: latlng,
      isHover: isHover,
      radius: radius,
      style: customStyle,
      rend3d: rend3d,
      geometry: new OlGeomCircle(fromLonLat(latlng), parseFloat(arcRadius))
    })
    this.feature = feature
    feature.setStyle(setOlStyleCircle(customStyle))
    this.vectorSource.addFeature(feature)
  }

  /**
   * @desc: 功能描述
   * @param {Number} id
   * @param {Array} latlng
   * @param {Number} type 1-航线中的航点半径 2-指点飞行的点半径 3-自定义点半径（如：基站） 4-吊舱视野位置区域 5-禁飞区 6-电子围栏
   * @param {Number} isHover
   * @param {Number} radius
   * @param {Object} style
   * @param {Boolean} rend3d
   * @return {*}
   * @use: 使用示例
   */
  addCircle2 (id, latlng, type, isHover = 1, radius = DEFAULT_CIRCLE_RADIUS, style = DEFAULT_CIRCLE_STYLE, rend3d = false) {
    let circleStyle = this.setCircleStyle(type, style)
    // 传过来的半径是几何半径（直线），需要转化为弧度半径（曲面），余弦公式推导
    let arcRadius = radius / Math.cos(latlng[1] / 180 * Math.PI)
    let feature = new OlFeature({
      id: id,
      sessionId: id,
      type: type,
      latlng: latlng,
      isHover: isHover,
      radius: radius,
      style: circleStyle,
      rend3d: rend3d,
      geometry: new OlGeomCircle(fromLonLat(latlng), parseFloat(arcRadius))
    })
    this.feature = feature
    if (type == 6) feature.setStyle(setOlStyleCircleNoFill(circleStyle))
    else feature.setStyle(setOlStyleCircle(circleStyle))
    this.vectorSource.addFeature(feature)
  }

  updateCircle2 (id, latlng, type, isHover, radius, style, rend3d = false, circleHashMap) {
    let circleStyle = this.setCircleStyle(type, style)
    let currentCircle = circleHashMap.get(id).item2d
    // 传过来的半径是几何半径（直线），需要转化为弧度半径（曲面），余弦公式推导
    let arcRadius = radius / Math.cos(latlng[1] / 180 * Math.PI)
    let feature = currentCircle.feature
    feature.setStyle(setOlStyleCircle(circleStyle))
    feature.getGeometry().setCenterAndRadius(fromLonLat(latlng), arcRadius)

    // 3d
    let currentCircle3d = circleHashMap.get(id).item3d
    currentCircle3d.position = new Cesium.Cartesian3.fromDegrees(latlng[0], latlng[1], latlng[2])
  }

  /**
   * @desc: 设置Circle样式
   * @param {Number} type
   * @param {Object} style
   * @return {*}
   * @use: 使用示例
   */
  setCircleStyle (type, style) {
    let circleStyle = {}
    if (!style) {
      switch (type) {
        case 0:
        case 1:
        case 2:
        case 3:
          circleStyle = DEFAULT_CIRCLE_STYLE
          break
        case 4:
          circleStyle = GREEN_CIRCLE_STYLE
          break
        case 5:
          circleStyle = FORBID_CIRCLE_STYLE
          break
        case 6:
          circleStyle = FENCE_CIRCLE_STYLE
          break
        default:
          circleStyle = DEFAULT_CIRCLE_STYLE
      }
    } else {
      circleStyle = style
    }
    return circleStyle
  }
}

export {
  Circle,
  Circle3d
}

